<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="生产主体" prop="producerName">
        <el-input
          v-model="queryParams.producerName"
          placeholder="请输入生产主体"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查时间" prop="inspectionTime">
        <el-date-picker clearable
                        v-model="queryParams.inspectionTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择检查时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="省" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区/县" prop="district">
        <el-input
          v-model="queryParams.district"
          placeholder="请输入区/县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['certificate:njt_inspection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['certificate:njt_inspection:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['certificate:njt_inspection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['certificate:njt_inspection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="njt_inspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      //基础信息
      <el-table-column label="基础信息" align="center">
      <el-table-column label="检查部门" align="center" prop="inspectionDepartment" />
      <el-table-column label="检查人员" align="center" prop="inspectorName" />
      <el-table-column label="生产主体" align="center" prop="producerName" />
      <el-table-column label="法人名称" align="center" prop="legalPersonName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="区/县" align="center" prop="district" />
      <el-table-column label="镇/乡" align="center" prop="township" />
      <el-table-column label="检查地点" align="center" prop="inspectionLocation" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      </el-table-column>
      //检查记录
      <el-table-column label="检查记录" align="center">
      <el-table-column label="合格证开具是否规范" align="center" prop="certificateCompliance" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.certificateCompliance === 1" type="success">合格</el-tag>
          <el-tag v-else type="danger">不合格</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否使用禁限用农药、兽药" align="center" prop="pesticideCompliance" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.pesticideCompliance === 1" type="success">合格</el-tag>
          <el-tag v-else type="danger">不合格</el-tag>
        </template>
      </el-table-column>
      </el-table-column>
      //其他记录
      <el-table-column label="其他记录" align="center" >
      <el-table-column label="检查意见" align="center" prop="inspectionComments" />
      <el-table-column label="检查记录照片" align="center" prop="inspectionPhotos" />
      <el-table-column label="检查时间" align="center" prop="inspectionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      </el-table-column>
      <el-table-column width="200" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['certificate:njt_inspection:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['certificate:njt_inspection:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农检通工作平台检查记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检查人员" prop="inspectorName">
          <el-input v-model="form.inspectorName" placeholder="请输入检查人员" />
        </el-form-item>
        <el-form-item label="检查部门" prop="inspectionDepartment">
          <el-input v-model="form.inspectionDepartment" placeholder="请输入检查部门" />
        </el-form-item>
<!--        <el-form-item label="打印编号" prop="inspectionDepartment">-->
<!--          <el-input v-model="form.inspectionDepartment" placeholder="请输入检查部门" />-->
<!--        </el-form-item>-->
        <el-form-item label="主体名称" prop="producerName">
          <el-input v-model="form.producerName" placeholder="请输入生产主体" />
        </el-form-item>
        <el-form-item label="法人" prop="legalPersonName">
          <el-input v-model="form.legalPersonName" placeholder="请输入法人名称" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="检查产品" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="检查地点" prop="inspectionLocation">
          <el-input v-model="form.inspectionLocation" placeholder="请输入检查地点" />
        </el-form-item>
        <el-form-item label="检查意见" prop="inspectionComments">
          <el-input v-model="form.inspectionComments" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="检查记录照片" prop="inspectionPhotos">
          <el-input v-model="form.inspectionPhotos" placeholder="请输入检查记录照片" />
        </el-form-item>
        <el-form-item label="检查时间" prop="inspectionTime">
          <el-date-picker clearable
                          v-model="form.inspectionTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择检查时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合格证开具是否规范" prop="certificateCompliance">
          <el-input v-model="form.certificateCompliance" placeholder="请输入合格证开具是否规范" />
        </el-form-item>
        <el-form-item label="是否使用禁限用农药、兽药" prop="pesticideCompliance">
          <el-input v-model="form.pesticideCompliance" placeholder="请输入是否使用禁限用农药、兽药" />
        </el-form-item>

        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNjt_inspection, getNjt_inspection, delNjt_inspection, addNjt_inspection, updateNjt_inspection } from "@/api/certificate/njt_inspection";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Njt_inspection",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农检通工作平台检查记录表格数据
      njt_inspectionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inspectionDepartment: null,
        inspectorName: null,
        producerName: null,
        legalPersonName: null,
        contactPhone: null,
        province: null,
        city: null,
        district: null,
        township: null,
        inspectionLocation: null,
        productName: null,
        certificateCompliance: null,
        pesticideCompliance: null,
        inspectionComments: null,
        inspectionPhotos: null,
        inspectionTime: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inspectionDepartment: [
          { required: true, message: "检查部门不能为空", trigger: "blur" }
        ],
        inspectorName: [
          { required: true, message: "检查人员不能为空", trigger: "blur" }
        ],
        producerName: [
          { required: true, message: "生产主体不能为空", trigger: "blur" }
        ],
        legalPersonName: [
          { required: true, message: "法人名称不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        province: [
          { required: true, message: "省不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "市不能为空", trigger: "blur" }
        ],
        district: [
          { required: true, message: "区/县不能为空", trigger: "blur" }
        ],
        township: [
          { required: true, message: "镇/乡不能为空", trigger: "blur" }
        ],
        inspectionLocation: [
          { required: true, message: "检查地点不能为空", trigger: "blur" }
        ],
        productName: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        certificateCompliance: [
          { required: true, message: "合格证开具是否规范不能为空", trigger: "blur" }
        ],
        pesticideCompliance: [
          { required: true, message: "是否使用禁限用农药、兽药不能为空", trigger: "blur" }
        ],
        inspectionTime: [
          { required: true, message: "检查时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询农检通工作平台检查记录列表 */
    getList() {
      this.loading = true;
      listNjt_inspection(this.queryParams).then(response => {
        this.njt_inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inspectionDepartment: null,
        inspectorName: null,
        producerName: null,
        legalPersonName: null,
        contactPhone: null,
        province: null,
        city: null,
        district: null,
        township: null,
        inspectionLocation: null,
        productName: null,
        certificateCompliance: null,
        pesticideCompliance: null,
        inspectionComments: null,
        inspectionPhotos: null,
        inspectionTime: null,
        createdAt: null,
        updatedAt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // 设置默认检查人员和检查部门
      this.form.inspectorName = 'admin';
      this.form.inspectionDepartment = '研发部门';
      this.open = true;
      this.title = "添加农检通工作平台检查记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNjt_inspection(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改农检通工作平台检查记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNjt_inspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNjt_inspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除农检通工作平台检查记录编号为"' + ids + '"的数据项？').then(function() {
        return delNjt_inspection(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('certificate/njt_inspection/export', {
        ...this.queryParams
      }, `njt_inspection_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
